/*
 *  OSCReceiver.java
 *
 *  copyright (c) 2006, tom dinchak & lorenzo orselli
 * 
 *  this file is part of jserialio.
 *
 *  jserialio is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  jserialio is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  You should have received a copy of the GNU General Public License
 *  along with jserialio; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 *  created on July 29, 2006, 1:55 AM
 *
 */

package serialio;

import java.io.*;



import com.illposed.osc.*;

/**
 *
 * @author tom
 */
public class OSCReceiver extends Thread {
    
    public LEDListener ledlistener;
    public LEDRowListener ledrowlistener;
    public LEDColListener ledcollistener;
    public IntensityListener intensitylistener;
    public TestListener testlistener;
    public ADCEnableListener adcenablelistener;
    public ShutdownListener shutdownlistener;
    public LEDResetListener resetlistener;

    OutputStream serialout;
    String OSCInPortStr;
    OSCPortIn inport;
    
    SerialIOProcess parent;
    
    boolean threadStop = false;
    /** Creates a new instance of OSCReceiver */
    public OSCReceiver(OutputStream newserialout, String newoscinportstr, SerialIOProcess parent) {
        this.serialout = newserialout;
        this.OSCInPortStr = newoscinportstr;
        this.parent = parent;
    }

    public String getStatus() {
    	if (threadStop == true) {
    		return "Stopped";
    	} else {
    		return "Started";
    	}
    }
    
    public void stopReceiver() throws java.net.SocketException {
        this.threadStop = true;
        
    }

    public void run() {
    	try {
    		runReceiver();
    	} catch (Exception e) {
    		parent.RestartOSCReceiver();
    		e.printStackTrace();
    	}
    }
    
    public void runReceiver() throws Exception {
        try {
            inport = new OSCPortIn(Integer.parseInt(this.OSCInPortStr));
          
            ledlistener = new LEDListener(serialout, parent);
            ledrowlistener = new LEDRowListener(serialout);
            ledcollistener = new LEDColListener(serialout);
            intensitylistener = new IntensityListener(serialout);
            testlistener = new TestListener(serialout);
            adcenablelistener = new ADCEnableListener(serialout);
            shutdownlistener = new ShutdownListener(serialout);
            resetlistener = new LEDResetListener(serialout);
            inport.addListener("/box/led", ledlistener);
            inport.addListener("/box/led_row", ledrowlistener);
            inport.addListener("/box/led_col", ledcollistener);
            inport.addListener("/box/intensity", intensitylistener);
            inport.addListener("/box/led_test", testlistener);
            inport.addListener("/box/adc_enable", adcenablelistener);
            inport.addListener("/box/shutdown", shutdownlistener);
            inport.addListener("/box/reset", resetlistener);
            inport.startListening();
            while (threadStop == false) {
                Thread.sleep(100);
            }
        	if (inport != null) {
        		inport.stopListening();
        		inport.close();
        	}
        	throw new Exception("Thread ended");
        } catch (InterruptedException e) {
           
        }
    }
}
